package org.skyeye.modeling.util;


import java.util.HashMap;
import java.util.Map;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;

/**
 * 
 * 棰滆壊宸ュ叿绫�
 * 
 * @author 閮厹
 * @date 2012-1-30
 * @modify xurq data 2012-08-21 寰楀埌棰滆壊宸ュ叿绫�
 */
public class ColorUtil {
	public static Color white = ColorConstants.white;
	public static Color lightGray = ColorConstants.lightGray;
	public static Color gray = ColorConstants.gray;
	public static Color black = ColorConstants.black;
	public static Color red = ColorConstants.red;
	public static Color darkGray = ColorConstants.darkGray;
	public static Color orange = ColorConstants.orange;
	public static Color yellow = ColorConstants.yellow;
	public static Color green = ColorConstants.green;
	public static Color lightGreen = ColorConstants.lightGreen;
	public static Color darkGreen = ColorConstants.darkGreen;
	public static Color cyan = ColorConstants.cyan;
	public static Color lightBlue = ColorConstants.lightBlue;
	public static Color blue = ColorConstants.blue;
	public static Color darkBlue = ColorConstants.darkBlue;
	private static Map<RGB, Color> colors = new HashMap<RGB, Color>();

	static {
		colors.put(white.getRGB(), white);
		colors.put(lightGray.getRGB(), lightGray);
		colors.put(gray.getRGB(), gray);
		colors.put(darkGray.getRGB(), darkGray);
		colors.put(black.getRGB(), black);
		colors.put(red.getRGB(), red);
		colors.put(orange.getRGB(), orange);
		colors.put(yellow.getRGB(), yellow);
		colors.put(green.getRGB(), green);
		colors.put(lightGreen.getRGB(), lightGreen);
		colors.put(darkGreen.getRGB(), darkGreen);
		colors.put(cyan.getRGB(), cyan);
		colors.put(lightBlue.getRGB(), lightBlue);
		colors.put(blue.getRGB(), blue);
		colors.put(darkBlue.getRGB(), darkBlue);
	}

	public static Color getColor(int red, int green, int blue) {
		RGB rgb = new RGB(red, green, blue);
		return getColor(rgb);
	}

	public static Color getColor(RGB rgb) {
		if (colors.containsKey(rgb)) {
			return colors.get(rgb);
		} else {
			Color color = new Color(null, rgb);
			colors.put(rgb, color);
			return color;
		}
	}

	public static void dispose() {
		for (Color color : colors.values()) {
			color.dispose();
		}
		colors.clear();
	}
	
	/**
	 * 灏哠tring绫诲瀷鐨凜olor.toString 杞崲涓篟GB绫诲瀷.
	 * @param str
	 * @return
	 */
	public static RGB getRGB(String str){
		String colorStr = str.substring(7,
				str.length() - 1);
		String[] colorList = colorStr.split(",");
		Color color=new Color(null, Integer
				.parseInt(colorList[0].trim()), Integer
				.parseInt(colorList[1].trim()), Integer
				.parseInt(colorList[2].trim()));
		RGB rgb=color.getRGB();
		return rgb;
	}
	
	/**
	 * 灏哠tring绫诲瀷鐨凜olor.toString 杞崲涓篊olor绫诲瀷.
	 * @param str
	 * @return
	 */
	public static Color getColor(String str){
		String colorStr = str.substring(7,
				str.length() - 1);
		String[] colorList = colorStr.split(",");
		Color color=new Color(null, Integer
				.parseInt(colorList[0].trim()), Integer
				.parseInt(colorList[1].trim()), Integer
				.parseInt(colorList[2].trim()));
		return color;
	}
}
